package com.cat.slidingWindow;

import java.util.Arrays;

/**
 * @author cat
 * @description https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/description/
 * @create 2025/8/1 10:33
 * @since JDK17
 */

public class Solution25 {
    static char[] vowel = {'a', 'e', 'i', 'o', 'u'};
    static int[] h = new int[26];
    int get(char c) {
        for (int i = 0; i < 5; i++) {
            if (c == vowel[i]) {
                return 0;
            }
        }
        return 1;
    }

    boolean judge(int cnt, int k) {
        if (cnt <= k) return false;
        for (int i = 0; i < 5; i++) {
            if (h[vowel[i] - 'a'] == 0) return false;
        }
        return true;
    }

    public long countOfSubstrings(String word, int k) {
        char[] s = word.toCharArray();
        return f(s, k - 1) - f(s, k);
    }

    long f(char[] s, int k) {
        Arrays.fill(h, 0);
        long ans = 0;
        int n = s.length, cnt = 0;
        for (int l = 0, r = 0; r < n; r++) {
            h[s[r] - 'a']++;
            cnt += get(s[r]);
            while (judge(cnt, k)) {
                ans += n - r;
                h[s[l] - 'a']--;
                cnt -= get(s[l++]);
            }
        }
        return ans;
    }
}
